<?php
namespace app\admin\model\logic;

use think\Model;

class Admin extends Model
{
    // 设置主键，默认id可以不用设置。
    // protected $pk = 'uid';
    protected $name = "admin";
    protected $admin;
    //设置模型初始化调用父级模型的初始化方法 
    public function __construct(){
        // $this->admin = new \app\admin\model\db\Admin; // 两者功效相等。
        $this->admin = app('\app\admin\model\db\Admin');
    }
    //获取带分页的列表方法
    public function get_admin_list_page(){
        $post_data=input();
        //判断是否有搜索条件
        if(isset($post_data['name'])&&!empty($post_data['name'])){
            $where['name']=['like','%'.$post_data['name'].'%'];
        }
        if(isset($post_data['role_id'])&&$post_data['role_id']>0){
            $where['role_id']=$post_data['role_id'];
        }else{
            $where['status']=['gt',0];
        }
        //设置排序规则
        $order['id']='desc';
        $field='id,login,name,add_time,status,role_id';
        $rs_arr['data']['list']= $this->admin->where($where)->order($order)->field($field)->paginate(20);
        $rs_arr['data']['page']=$rs_arr['data']['list']->render();
        $new_where=['status'=>['gt',0]];
        $rs_arr['count']= $this->admin->where($new_where)->count();
        return $rs_arr;
    }
    //保存管理员
    public function admin_save_add()
    {
        $post_data=input();
        $validate=validate('admin');
        if($validate->scene('add')->check($post_data)){
            $post_data['mobile']=$post_data['login'];
            $pass_arr=generate_passwd($post_data['password']);
            $post_data['password']=$pass_arr['pass'];
            $post_data['password_code']=$pass_arr['pass_code'];
            $rs_st=$this->admin->allowField(true)->isUpdate(false)->save($post_data);
            if($rs_st!=false){
                $rs_arr['code']=1;
                $rs_arr['msg']='添加成功';
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='添加失败';
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //保存管理员编辑
    public function admin_save_edit()
    {
        $post_data=input();
        $validate=validate('admin');
        if($post_data['role_id']==1){//判断是否为超级管理员 如果是超级管理员就不验证登录名123
            $is_check=$validate->scene('base')->check($post_data);
        }else{
            $is_check=$validate->scene('edit')->check($post_data);
        }
        if($is_check){
            $post_data['mobile']=$post_data['login'];
            if(!empty($post_data['password'])){
                $pass_arr=generate_passwd($post_data['password']);
                $post_data['password']=$pass_arr['pass'];
                $post_data['password_code']=$pass_arr['pass_code'];
            }else{
                unset($post_data['password']);
            }
            $rs_st=$this->admin->allowField(true)->isUpdate(true)->save($post_data);
            if($rs_st!=false){
                $rs_arr['code']=1;
                $rs_arr['msg']='编辑成功';
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='编辑失败';
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //管理员登录
    public function admin_login()
    {
        $post_data=input();
        $validate=validate('admin');
        if($validate->scene('login')->check($post_data)){
            if (!captcha_check($post_data['captcha'])) {
                $rs_arr['code']=0;
                $rs_arr['msg']='验证码不正确';
            }else{
                $login_where['login']=$post_data['login'];
                $login_where['status']=1;
                $rs_row=$this->admin->where($login_where)->find();
                if(empty($rs_row)){
                    $rs_arr['code']=0;
                    $rs_arr['msg']='您还没注册';
                }else{
                    $new_pass=diff_passwd($post_data['password'],$rs_row['password_code']);
                    if($new_pass==$rs_row['password']){
                        if($rs_row['role_id']==12||$rs_row['role_id']==13){
                            $shop_list=model('logic.Shop')->get_shop_all( $rs_row['id']);
                            if($rs_row['role_id']==12){
                                if(count($shop_list)<1){
                                    $rs_arr['code']=0;
                                    $rs_arr['msg']='生态圈店铺不存在';
                                }else{
                                    cookie("adminid", $rs_row['id']);
                                    cookie("role", $rs_row['role_id']);
                                    app('app\admin\model\logic\AdminLog')->record_log();
                                    $up_data['id']=$rs_row['id'];
                                    $up_data['last_login_time']=time();
                                    $up_data['last_login_ip']=request()->ip();
                                    $this->admin->allowField(true)->isUpdate(true)->save($up_data);
                                    $rs_arr['code']=1;
                                    $rs_arr['data']=$rs_row['id'];
                                    $rs_arr['msg']='登录成功';
                                }
                            }else{
                                if(count($shop_list)<2){
                                    $rs_arr['code']=0;
                                    $rs_arr['msg']='刀圈店铺不存在';
                                }else{
                                    cookie("adminid", $rs_row['id']);
                                    cookie("role", $rs_row['role_id']);
                                    app('app\admin\model\logic\AdminLog')->record_log();
                                    $up_data['id']=$rs_row['id'];
                                    $up_data['last_login_time']=time();
                                    $up_data['last_login_ip']=request()->ip();
                                    $this->admin->allowField(true)->isUpdate(true)->save($up_data);
                                    $rs_arr['code']=1;
                                    $rs_arr['data']=$rs_row['id'];
                                    $rs_arr['msg']='登录成功';
                                }
                            }
                        }else{
                            cookie("adminid", $rs_row['id']);
                            cookie("role", $rs_row['role_id']);
                            dump(123);
                            app('app\admin\model\logic\AdminLog')->record_log();
                            dump(321);
                            $up_data['id']=$rs_row['id'];
                            $up_data['last_login_time']=time();
                            $up_data['last_login_ip']=request()->ip();
                            $this->admin->allowField(true)->isUpdate(true)->save($up_data);
                            $rs_arr['code']=1;
                            $rs_arr['data']=$rs_row['id'];
                            $rs_arr['msg']='登录成功';
                        }
                    }else{
                        $rs_arr['code']=0;
                        $rs_arr['msg']='密码错误请核实后再试';
                    }
                }
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //验证管理员信息
    public function validate_admin(&$id)
    {
        $where['id']=$id;
        $where['status']=1;
        $validate=validate('admin');
        if($validate->scene('validate_admin')->check($where)){
            $rs_row=$this->admin->where($where)->find();
            if(empty($rs_row)){
                $rs_arr['code']=0;
            }else{
                $rs_arr['code']=1;
                $rs_arr['data']=$rs_row;
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //管理员重置密码方法
    public function user_reset_pass()
    {
        $post_data=input();
        $validate=validate('user');
        if($validate->scene('login')->check($post_data)){
            $rs_row=$this->user->where(['user_login'=>$post_data['user_login']])->find();
            if(empty($rs_row)){
                $rs_arr['code']=0;
                $rs_arr['msg']='您还没注册';
            }else{
                $new_pass=diff_passwd($post_data['pass'],$rs_row['pass_code']);
                if($new_pass==$rs_row['pass']){
                    $rs_arr['code']=0;
                    $rs_arr['msg']='新密码不能和原密码相同';
                }else{
                    $up_data['id']=$rs_row['id'];
                    $pass_arr=generate_passwd($post_data['pass']);
                    $up_data['pass']=$pass_arr['pass'];
                    $up_data['pass_code']=$pass_arr['pass_code'];
                    $rs_st=$this->user->allowField(true)->isUpdate(true)->save($up_data);
                    if($rs_st!=false){
                        $rs_arr['code']=1;
                        $rs_arr['msg']='密码修改成功';
                    }else{
                        $rs_arr['code']=0;
                        $rs_arr['msg']='密码修改失败';
                    }
                }
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //忘记密码
    public function reset_passwd(){
        $post_data=input();
        $validate=validate('admin');
        if($validate->scene('login')->check($post_data)){
            //验证code
            $rs = check_code($post_data['login'],$post_data['code']);
            if($rs['code']==0){
                return $rs;
            }
            $rs_row=$this->admin->where(['login'=>$post_data['login']])->find();
            if(empty($rs_row)){
                $rs_arr['code']=0;
                $rs_arr['msg']='您还没注册';
            }else{
                $new_pass=diff_passwd($post_data['password'],$rs_row['password_code']);
                if($new_pass==$rs_row['password']){
                    $rs_arr['code']=0;
                    $rs_arr['msg']='新密码不能和原密码相同';
                }else{
                    $up_data['id']=$rs_row['id'];
                    $pass_arr=generate_passwd($post_data['password']);
                    $up_data['password']=$pass_arr['pass'];
                    $up_data['password_code']=$pass_arr['pass_code'];
                    $rs_st=$this->admin->allowField(true)->isUpdate(true)->save($up_data);
                    if($rs_st!=false){
                        $rs_arr['code']=1;
                        $rs_arr['msg']='密码修改成功';
                        $rs_arr['url']=url('index');
                    }else{
                        $rs_arr['code']=0;
                        $rs_arr['msg']='密码修改失败';
                    }
                }
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //获取管理员信息
    public function get_admin_info()
    {
        $post_data=input();
        $validate=validate('user');
        if($validate->scene('id')->check($post_data)){
            $rs_row=$this->admin->where(['id'=>$post_data['id']])->find();
            if(empty($rs_row)){
                $rs_arr['code']=0;
                $rs_arr['msg']='未找到对应用户信息';
            }else{
                $rs_data['id']=$rs_row->id;
                $rs_data['user_sn']=$rs_row->user_sn;
                $rs_data['user_type']=$rs_row->user_type;
                $rs_data['head_img']=$rs_row->head_img;
                $rs_data['user_nick']=$rs_row->user_nick;
                $rs_data['score']=$rs_row->score;//剩余积分
                $rs_data['user_money']=$rs_row->user_money;//账户余额
                $rs_data['coupon']=0;//优惠券数量
                $rs_sign=model('logic.userSign')->day_sgin($post_data['id']);
                if($rs_sign){
                    $rs_data['sign']=0;//未签到
                }else{
                    $rs_data['sign']=1;//已签到
                }
                $rs_arr['code']=0;
                $rs_arr['data']=$rs_data;
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //根据管理员ID获取角色ID
    public function get_admin_role_id(&$id)
    {
        $admin_row=$this->admin->where(['id'=>$id]);
        if(empty($admin_row)){
            $rs_arr['code']=0;
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$admin_row['role_id'];
        }
        return $rs_arr;
    }
    //判断角色下是否有管理员
    public function get_role_admin($role_id)
    {
        $admin_row=$this->admin->where(['role_id'=>$role_id])->find();
        if(empty($admin_row)){
            $rs_arr['code']=1;
        }else{
            $rs_arr['code']=0;
            $rs_arr['data']='存在该角色管理员不能删除';
        }
        return $rs_arr;
    }
    //获取管理员信息
    public function get_admin_row(&$where)
    {
        $admin_row=$this->admin->get($where);
        if(empty($admin_row)){
            $rs_arr['code']=0;
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$admin_row;
        }
        return $rs_arr;
    }
    //删除管理员
    public function admin_save_del()
    {
        $post_data=input();
        $validate=validate('admin');
        if($validate->scene('del')->check($post_data)){
            $admin_arr=json_decode($post_data['id_list'],true);
            foreach($admin_arr as $ve){
                $rs_st=$this->admin->where(['id'=>$ve])->delete();
            }
            if($rs_st!=false){
                $rs_arr['code']=1;
                $rs_arr['msg']='删除成功';
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']='删除失败';
            }
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']=$validate->getError();
        }
        return $rs_arr;
    }
    //获取未分配店铺的管理员列表
    public function get_shop_admin()
    {
        $where['role_id']=3;
        $where['shop_id']=1;
        $rs_list=$this->admin->get_list($where);
        if(empty($rs_list)){
            $rs_arr['code']=0;
            $rs_arr['msg']='没有获取到店铺管理员';
        }else{
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_list;
        }
        return $rs_arr;
    }
    //店铺入住申请审核通过 给对应的用户生成管理员账号
    public function produce_admin($shop_id,$user_id,$type)
    {
        $user_row=model('logic.user')->get_row($user_id);
        $in_data['login']=$user_row['data']['user_login'];
        $in_data['password']=$user_row['data']['passwd'];
        $in_data['password_code']=$user_row['data']['passwd_code'];
        $in_data['name']=$user_row['data']['user_name'];
        $in_data['mobile']=$user_row['data']['mobile'];
        $admin_where['login']=$in_data['login'];
        $rs_admin_row=$this->admin->get($admin_where);
        if(empty($rs_admin_row)){
            if($type==1){
                $in_data['role_id']=13;
            }else{
                $in_data['role_id']=12;
            }
            $validate=validate('admin');
            if($validate->scene('produce_admin')->check($in_data)){
                $rs_st=$this->admin->allowField(true)->isUpdate(false)->save($in_data);
                if($rs_st!=false){
                    $rs_arr['code']=1;
                    $rs_arr['data']=$this->admin->id;
                    $rs_arr['msg']='添加成功';
                }else{
                    $rs_arr['code']=0;
                    $rs_arr['msg']='添加失败';
                }
            }else{
                $rs_arr['code']=0;
                $rs_arr['msg']=$validate->getError();
            }
        }else{
            $up_data['id']=$rs_admin_row['id'];
            $up_data['role_id']=13;
            $rs_st=$this->admin->allowField(true)->isUpdate(true)->save($up_data);
            $rs_arr['code']=1;
            $rs_arr['data']=$rs_admin_row['id'];
            $rs_arr['msg']='添加成功';
        }
        return $rs_arr;
    }
    //后台给管理员添加店铺成功 修改对应管理员shopid
    public function up_shop_admin($shop_id,$admin_id)
    {
        $up['id']=$admin_id;
        $up['shop_id']=$shop_id;
        $rs_st=$this->admin->allowField(true)->isUpdate(true)->save($up);
        if($rs_st!=false){
            $rs_arr['code']=1;
            $rs_arr['msg']='更新成功';
        }else{
            $rs_arr['code']=0;
            $rs_arr['msg']='更新失败';
        }
        return $rs_arr;
    }
}